﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Domain.Values
{
	using System;
	using System.Web;
	using System.Web.Mvc;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Core.Validators;

	public class UserBadgeCreate : ValueBase<UserBadgeCreate>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserBadgeCreate"/> class.
		/// </summary>
		public UserBadgeCreate()
		{
			this.UserBadgeService = Application.Resolve<IUserBadgeService>();
			this.Reputation = 0;
			this.Existing = false;
		}

		/// <summary>
		/// Gets or sets the user badge repository.
		/// </summary>
		/// <value>The user badge repository.</value>
		public IUserBadgeService UserBadgeService
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the id.
		/// </summary>
		/// <value>The id.</value>
		public int Id
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the name.
		/// </summary>
		/// <value>The name.</value>
		public string Name
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the title.
		/// </summary>
		/// <value>The title.</value>
		public string Title
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the body.
		/// </summary>
		/// <value>The body.</value>
		public string Body
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the reputation.
		/// </summary>
		/// <value>The reputation.</value>
		public int Reputation
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets a value indicating whether this <see cref="UserBadgeCreate"/> is existing.
		/// </summary>
		/// <value><c>true</c> if existing; otherwise, <c>false</c>.</value>
		public bool Existing
		{
			get; 
			set;
		}

		/// <summary>
		/// Gets or sets the scope.
		/// </summary>
		/// <value>The scope.</value>
		public UserBadgeScope Scope
		{
			get;
			set;
		}

		/// <summary>
		/// Gets or sets the source.
		/// </summary>
		/// <value>The source.</value>
		public HttpPostedFileBase Source
		{
			get;
			set;
		}

		/// <summary>
		/// Gets the badges.
		/// </summary>
		/// <value>The badges.</value>
		public SelectList Badges
		{
			get
			{
				var badges = UserBadgeService.GetGrouped(new UserBadgeSpecification
				{
					Page = 1, 
					Limit = int.MaxValue
				});

				badges.Items.Add(new UserBadge
				{
					Title = Resources.Titles.SelectBadge
				});

				return new SelectList(badges, "Id", "Title", this.Id);
			}
		}

		/// <summary>
		/// Gets the scopes.
		/// </summary>
		/// <value>The scopes.</value>
		public SelectList Scopes
		{
			get
			{
				return new SelectList(Enum.GetNames(typeof(UserBadgeScope)), this.Scope.Name());
			}
		}

		/// <summary>
		/// Validation rules for this instance.
		/// </summary>
		/// <returns></returns>
		public override Validator<UserBadgeCreate> Rules()
		{
			var rules = base.Rules();

			rules.Add(x => x.Name)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Name));

			rules.Add(x => x.Name)
				.Custom(new UserNameMustExistValidator(), Resources.Errors.UserNameNotFound);

			if (this.Existing)
			{
				if (this.Id == 0)
				{
					rules.AddError("Id", Resources.Errors.Required.Fmt(Resources.Titles.Badge));
				}

				return rules;
			}

			rules.Add(x => x.Title)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Title));

			rules.Add(x => x.Title)
				.Custom(new UserBadgeUniqueTitleValidator(), Resources.Errors.UserBadgeTitleTaken);

			rules.Add(x => x.Body)
				.NotEmpty(Resources.Errors.Required.Fmt(Resources.Titles.Description));

			return rules;
		}

		/// <summary>
		/// Validates this instance.
		/// </summary>
		public override void Validate()
		{
			this.Rules().Validate(this);
		}
	}
}